﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Collections;

namespace Event_SG_EWDT
{
    public partial class ViewEvent : System.Web.UI.Page
    {
        string eventid = "";


        protected void Page_Load(object sender, EventArgs e)
        {
                //if (!User.Identity.IsAuthenticated)

            if (!IsPostBack)
            {
                if (Request.Url != null)
                {
                    Session["PreviousPageUrl"] = Request.Url.ToString();
                }
                else
                {
                    Session["PreviousPageUrl"] = "~/index.aspx";
                }
            }
            
            if (Session["flag"] != null)
            {
                switch (Session["flag"].ToString())
                {
                    case "login":
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Welcome!');", true);
                        break;

                    case "firsttime":
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Welcome! You have been registered!');", true);
                        break;

                    case "created":
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Yay! Your event has been created!');", true);
                        break;

                    case "joined":
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Yay! You have joined the event!');", true);
                        break;

                    case "leave":
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Yay! You have left the event!');", true);
                        break;

                    case "updated":
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Yay! Your event has been updated!');", true);
                        break;

                    case "pending":
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Yay! You are now waiting to be accepted to the event!');", true);
                        break;

                    case "invited":
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Yay! You have invited friends to join the event!');", true);
                        break;
                }

                Session["flag"] = null;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "", true);
            }

                eventid = Request.QueryString["eventid"];


                if (Session["Member"] == null || eventid == null)
                {
                    Session["flag"] = "redirecterror";
                    Response.Redirect("~/registration.aspx");
                }

                //do a sql to get event details
            
                Event ev = EventControl.RetrieveEvent(eventid);

                if (ev.EventId == null)
                {
                    Session["flag"] = "notfound";
                    Response.Redirect("~/index.aspx");
                }



                lblViewEventDetailsTitle.Text = ev.EventTitle;
                tbxViewEventDesc.Text = ev.Description;
                tbxViewEventSdate.Text = ev.StartTime.ToString();
                tbxViewEventEdate.Text = ev.EndTime.ToString();
                tbxViewEventFee.Text = ev.EventFee.ToString();
                tbxViewEventLocation.Text = ev.Location;
                tbxViewEventCategory.Text = EventControl.GetCategoryName(ev.CategoryId);


                //show image....
                //imgViewEventPoster.ImageUrl = Utility.GetEventPosterPath(Server, ev.EventId);
                string id = DBMANAGER.GetImageIdEventPoster(eventid);
               
                imgViewEventPoster.ImageUrl = "~/HTTPHandler.ashx?imageid=" + Convert.ToInt32(id);

                #region ShowJoinButton
                //do a sql to get if user joined the event
                EventControl.RetrieveMemberEvent(Session["Member"].ToString());

                    btnInviteEvent.Visible = false;

                if (EventControl.checkIfJoined(Session["Member"].ToString(),eventid))
                {
                    btnJoinEvent.Visible = false;
                    btnLeaveEvent.Visible = true;

                    if (EventControl.IsAdmin(Session["Member"].ToString(), eventid))   // Is admin or not 
                    {
                        btnEditEvent.Visible = true;
                       // btnInviteEvent.Visible = true;
                        btnLeaveEvent.Visible = false;
                    }
                    else
                    {
                        btnEditEvent.Visible = false;
                       // btnInviteEvent.Visible = false;
                    }
                    
                }
                else
                {
                    btnJoinEvent.Visible = true;
                }
                if (ev.Privacy == 2)
                {
                    btnJoinEvent.Visible = false;
                }
                
                int status =EventControl.RetrieveMemberEvent_Status(Session["Member"].ToString(), eventid);
                
            
                if (status == 4)
                {
                    btnJoinEvent.Visible = true;
                    btnJoinEvent.Enabled = false;
                    btnJoinEvent.Text = "Waiting";
                    btnLeaveEvent.Visible = false;
                }
                else if (status == 3)
                {  // waiting to reply
                    btnJoinEvent.Visible = true;

                }
                else if (status == 1)
                {
                    btnJoinEvent.Visible = false;
                    btnLeaveEvent.Visible = false;
                }
                else if ( status == 0)
                {
                    btnInviteEvent.Visible = true;
                }
                #endregion

                string twitterurl = Utility.GetServerUrl(Request) + "/ViewEvent.aspx?eventid={0}";

            //twitter.Attributes.Add("data-url", String.Format(twitterurl,eventid));

            twitter.Attributes["data-url"] = String.Format(twitterurl, eventid); // "http://www.eventatsg.com/index.aspx";

            //twitter.Attributes["data-url"] = "http://localhost:26473/ViewEvent.aspx?eventid=10";
        }

        protected void btnJoinEvent_Click(object sender, EventArgs e)
        {
            //0 is joined, 99 is pending
            int status = EventControl.JoinEvent(Session["Member"].ToString(), eventid);

            //Create notification to event creater;

            int s = Convert.ToInt32(Session["Member"]);
            int r = EventControl.FindAdmin(eventid);

            Utility.SendJoinInterestToEventAdmin(s, r, eventid);

            if (status == 0)
            {
                Session["flag"] = "joined";
                Response.Redirect("~/ViewEvent.aspx" + "?eventid=" + eventid);
            }
            else if (status == 99)
            {
                Session["flag"] = "pending";
                Response.Redirect("~/ViewEvent.aspx" + "?eventid=" + eventid);
            }
        }

        protected void btnEditEvent_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/UpdateEvent.aspx"+"?eventid=" + eventid);
        }

        protected void btnLeaveEvent_Click(object sender, EventArgs e)
        {
                //do a sql to allow user to leave this event 
                EventControl.LeaveEvent(Session["Member"].ToString(),eventid);
                Session["flag"] = "leave";
                Response.Redirect("~/ViewEvent.aspx" + "?eventid=" + eventid);
            
        }

        protected void btnInviteEvent_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/InvitePplToEvent.aspx" + "?eventid=" + eventid);
        }

        
    }
}